import 'package:flutter/material.dart';
import 'package:geomancy/widgets/common_loading_circle.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';


class CustomFreshHeader extends StatefulWidget {
  final RefreshController refreshController;
  const CustomFreshHeader({super.key, required this.refreshController});

  @override
  State<CustomFreshHeader> createState() => _CustomFreshHeaderState();
}

class _CustomFreshHeaderState extends State<CustomFreshHeader> with TickerProviderStateMixin {
  late AnimationController _anicontroller, _scaleController;
  double _offset = 0.0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _anicontroller = AnimationController(vsync: this, duration: const Duration(milliseconds: 2000));
    _scaleController = AnimationController(value: 0.0, vsync: this, upperBound: 1.0);
    widget.refreshController.headerMode!.addListener(() {
      if (widget.refreshController.headerStatus == RefreshStatus.idle) {
        _scaleController.value = 0.0;
        _anicontroller.reset();
      } else if (widget.refreshController.headerStatus == RefreshStatus.refreshing) {
        _anicontroller.repeat();
      }
    });
  }

  @override
  void dispose() {
    _anicontroller.dispose();
    _scaleController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CustomHeader(
      refreshStyle: RefreshStyle.Behind,
      onOffsetChange: (offset) {
        if (widget.refreshController.headerMode!.value != RefreshStatus.refreshing) {
          _scaleController.value = offset / 80.0;
          setState(() {
            _offset = offset;
          });
        }
      },
      builder: (context, m) {
        return Container(
          alignment: Alignment.center,
          child: _offset > 0
              ? const CommonLoadingCircleWidget()
              : Container(),
        );
      },
    );
  }
}

